﻿//Copyright (c) 2009, Offbeat Guides
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//* Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other materials 
//provided with the distribution.
//
//* Neither the name of Offbeat Guides nor the names of its contributors may be used to 
//endorse or promote products derived from this software without specific prior written 
//permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Amazon.Services.S3;
using Amazon.Services;
using Amazon.WebServices.UnitTest.Properties;

namespace Amazon.WebServices.UnitTest
{
    /// <summary>
    /// Summary description for S3UnitTests
    /// </summary>
    [TestClass]
    public class S3UnitTests
    {
        [TestMethod]
        public void DriveInfoTest()
        {
            AmazonAccount account = new AmazonAccount(Settings.Default.AccessID, Settings.Default.SecretAccessID);

            if (DriveInfo.Exists(account, "Test_debug"))
                DriveInfo.Delete(account, "Test_debug", true);

            DriveInfo[] drives1 = DriveInfo.GetDrives(account);
            Assert.IsTrue(!DriveInfo.Exists(account, "Test_debug"));

            DriveInfo.Create(account, "Test_debug");

            DriveInfo[] drives2 = DriveInfo.GetDrives(account);

            Assert.AreEqual(drives1.Length + 1, drives2.Length);
            Assert.IsTrue(DriveInfo.Exists(account, "Test_debug"));

            DriveInfo.Delete(account, "Test_debug", true);
            Assert.IsTrue(!DriveInfo.Exists(account, "Test_debug"));
        }

        [TestMethod]
        public void DirectoryTest()
        {
            AmazonAccount account = new AmazonAccount(Settings.Default.AccessID, Settings.Default.SecretAccessID);

            if (!DriveInfo.Exists(account, "Test_debug"))
                DriveInfo.Create(account, "Test_debug");

            if (Directory.Exists(account, "Test_debug:\\sub1\\"))
                Directory.Delete(account, "Test_debug:\\sub1\\", true);

            Assert.IsFalse(Directory.Exists(account, "Test_debug:\\sub1\\"));

            Directory.CreateDirectory(account, "Test_debug:\\sub1\\");
            Assert.IsTrue(Directory.Exists(account, "Test_debug:\\sub1\\"));

            Directory.CreateDirectory(account, "Test_debug:\\sub1\\sub2\\");
            Assert.IsTrue(Directory.Exists(account, "Test_debug:\\sub1\\sub2\\"));

            String[] directories = Directory.GetDirectories(account, "Test_debug:\\sub1\\");
            Assert.IsTrue(directories.Length == 1);

            String[] files = Directory.GetFiles(account, "Test_debug:\\sub1\\");
            Assert.IsTrue(files.Length == 0);

            Directory.Delete(account, "Test_debug:\\sub1\\", true);
            Assert.IsFalse(Directory.Exists(account, "Test_debug:\\sub1\\"));
        }

        [TestMethod]
        public void DirectoryInfoTest()
        {
            AmazonAccount account = new AmazonAccount(Settings.Default.AccessID, Settings.Default.SecretAccessID);

            if (!DriveInfo.Exists(account, "Test_debug"))
                DriveInfo.Create(account, "Test_debug");

            if (Directory.Exists(account, "Test_debug:\\sub1\\"))
                Directory.Delete(account, "Test_debug:\\sub1\\", true);

            DirectoryInfo dir = new DirectoryInfo(account, "Test_debug:\\sub1\\");
            Assert.IsFalse(dir.Exists);

            dir.Create();
            Assert.IsTrue(dir.Exists);

            DirectoryInfo dir2 = dir.CreateSubdirectory("sub2");
            Assert.IsTrue(dir2.Exists);

            DirectoryInfo[] subdirs = dir.GetDirectories();
            Assert.IsTrue(subdirs.Length == 1);

            subdirs = dir2.GetDirectories();
            Assert.IsTrue(subdirs.Length == 0);

            dir.Delete(true);
            Assert.IsFalse(dir.Exists);
            Assert.IsFalse(Directory.Exists(account,"Test_debug:\\sub1\\"));
        }

        [TestMethod]
        public void FileInfoTest()
        {
            AmazonAccount account = new AmazonAccount(Settings.Default.AccessID, Settings.Default.SecretAccessID);

            if (!DriveInfo.Exists(account, "Test_debug"))
                DriveInfo.Create(account, "Test_debug");

            if (File.Exists(account, "Test_debug:\\testdoc.txt"))
                File.Delete(account, "Test_debug:\\testdoc.txt");

            FileInfo file = new FileInfo(account, "Test_debug:\\testdoc.txt");
            Assert.IsFalse(file.Exists);

            Byte[] buffer = System.Text.Encoding.Default.GetBytes("Hello World!");
            using (FileStream stream = file.Create())
            {
                stream.Write(buffer, 0, buffer.Length);
                stream.Flush();
            }
            file.Refresh();
            Assert.IsTrue(file.Exists);

            file.Delete();
            Assert.IsFalse(file.Exists);
            Assert.IsFalse(File.Exists(account, "Test_debug:\\testdoc.txt"));
        }

        [TestMethod]
        public void FileStreamTest()
        {
            AmazonAccount account = new AmazonAccount(Settings.Default.AccessID, Settings.Default.SecretAccessID);

            if (!DriveInfo.Exists(account, "Test_debug"))
                DriveInfo.Create(account, "Test_debug");
            if (Directory.Exists(account, "Test_debug:\\sub1\\"))
                Directory.Delete(account, "Test_debug:\\sub1\\", true);

            Byte[] buffer = System.Text.Encoding.UTF8.GetBytes("Hello World!");

            FileStream stream = new FileStream(account, "Test_debug:\\sub1\\test.txt", System.IO.FileMode.Create);
            stream.Write(buffer, 0, buffer.Length);
            stream.Flush();
            stream.Close();

            String text = File.ReadAllText(account, "Test_debug:\\sub1\\test.txt");
            Assert.AreEqual<String>(text, "Hello World!");
        }

        [TestMethod]
        public void PathTest()
        {
            String path = Path.Combine("bucket:", "\\dir1\\dir2\\test.txt");
            Assert.AreEqual(path, "bucket:\\dir1\\dir2\\test.txt");
        }
    }
}
